<?php
/**
* This functions common to most pages
* @author Nick Korbel <lqqkout13@users.sourceforge.net>
* @version 09-01-04
* @package phpScheduleIt
*
* Copyright (C) 2003 - 2005 phpScheduleIt
* License: GPL, see LICENSE
*/
/**
* Base directory of application
*/
@define('BASE_DIR', dirname(__FILE__) . '/..');
/**
* Include configuration file
**/
include_once(BASE_DIR . '/config/config.php');
/**
* Include Link class
*/
include_once('Link.class.php');
/**
* Include Pager class
*/
include_once('Pager.class.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/ernie/lib/php/common/Common.utils.php');


/**
* Provides functions common to most pages
*/
class CmnFns {

	/**
	* Convert minutes to hours
	* @param double $time time to convert in minutes
	* @return string time in 12 hour time
	*/
	function formatTime($time) {
		global $conf;

		// Set up time array with $timeArray[0]=hour, $timeArray[1]=minute
		// If time does not contain decimal point
		// then set time array manually
		// else explode on the decimal point
		$hour = intval($time / 60);
		$min = $time % 60;
		if ($conf['app']['timeFormat'] == 24) {
			$a = '';									// AM/PM does not exist
			if ($hour < 10) $hour = '0' . $hour;
		}
		else {
			$a = ($hour < 12 || $hour == 24) ? translate('am') : translate('pm');			// Set am/pm
			if ($hour > 12) $hour = $hour - 12;			// Take out of 24hr clock
			if ($hour == 0) $hour = 12;					// Don't show 0hr, show 12 am
		}
		// Set proper minutes (the same for 12/24 format)
		if ($min < 10) $min = 0 . $min;
		// Put into a string and return
		return $hour . ':' . $min . $a;
	}


	/**
	* Convert timestamp to date format
	* @param string $date timestamp
	* @param string $format format to put datestamp into
	* @return string date as $format or as default format
	*/
	function formatDate($date, $format = '') {
		global $dates;

		if (empty($format)) $format = $dates['general_date'];
		return strftime($format, $date);
	}


	/**
	* Convert UNIX timestamp to datetime format
	* @param string $ts MySQL timestamp
	* @param string $format format to put datestamp into
	* @return string date/time as $format or as default format
	*/
	function formatDateTime($ts, $format = '') {
		global $conf;
		global $dates;

		if (empty($format))
			$format = $dates['general_datetime'] . ' ' . (($conf['app']['timeFormat'] ==24) ? '%H' : '%I') . ':%M:%S' . (($conf['app']['timeFormat'] == 24) ? '' : ' %p');
		return strftime($format, $ts);
	}


	/**
	* Convert minutes to hours/minutes
	* @param int $minutes minutes to convert
	* @return string version of hours and minutes
	*/
	function minutes_to_hours($minutes) {
		if ($minutes == 0)
			return '0 ' . translate('hours');

		$hours = (intval($minutes / 60) != 0) ? intval($minutes / 60) . ' ' . translate('hours') : '';
		$min = (intval($minutes % 60) != 0) ? intval($minutes % 60) . ' ' . translate('minutes') : '';
		return ($hours . ' ' . $min);
	}

	/**
	* Return the current script URL directory
	* @param none
	* @return url url of curent script directory
	*/
	function getScriptURL() {
		global $conf;
		$uri = $conf['app']['weburi'];
		return (strrpos($uri, '/') === false) ? $uri : substr($uri, 0, strlen($uri));
	}


	/**
	* Prints an error message box and kills the app
	* @param string $msg error message to print
	* @param string $style inline CSS style definition to apply to box
	* @param boolean $die whether to kill the app or not
	*/
	function do_error_box($msg, $style='', $die = true) {
		global $conf;

		echo '<table border="0" cellspacing="0" cellpadding="0" align="center" class="alert" style="' . $style . '"><tr><td>' . $msg . '</td></tr></table>';

		if ($die) {
			echo '</td></tr></table>';		// endMain() in Template
			echo '<p align="center"><a href="http://phplabit.sourceforge.net">phpScheduleIt v' . $conf['app']['version'] . '</a></p></body></html>';	// printHTMLFooter() in Template
		 	die();
		}
	}

	/**
	* Prints out a box with notification message
	* @param string $msg message to print out
	* @param string $style inline CSS style definition to apply to box
	*/
	function do_message_box($msg, $style='') {
		echo '<table border="0" cellspacing="0" cellpadding="0" align="center" class="message" style="' . $style . '"><tr><td>' . $msg . '</td></tr></table>';
	}

	/**
	* Returns a reference to a new Link object
	* Used to make HTML links
	* @param none
	* @return Link object
	*/
	function getNewLink() {
		return new Link();
	}

	/**
	* Returns a reference to a new Pager object
	* Used to iterate over limited recordesets
	* @param none
	* @return Pager object
	*/
	function getNewPager() {
		return new Pager();
	}

	/**
	* Strip out slahses from POST values
	* @param none
	* @return array of cleaned up POST values
	*/
	function cleanPostVals() {
		$return = array();

		foreach ($_POST as $key => $val)
			$return[$key] = stripslashes(trim($val));

		return $return;
	}

	/**
	* Strip out slahses from an array of data
	* @param none
	* @return array of cleaned up data
	*/
	function cleanVals($data) {
		$return = array();

		foreach ($data as $key => $val)
			$return[$key] = stripslashes($val);

		return $return;
	}

	/**
	* Verifies vertical order and returns value
	* @param string $vert value of vertical order
	* @return string vertical order
	*/
	function get_vert_order($get_name = 'vert') {
		// If no vertical value is specified, use ASC
		$vert = isset($_GET[$get_name]) ? $_GET[$get_name] : 'ASC';

		// Validate vert value, default to DESC if invalid
		switch($vert) {
			case 'DESC';
			case 'ASC';
			break;
			default :
				$vert = 'DESC';
			break;
		}

		return $vert;
	}

	/**
	* Verifies and returns the order to list recordset results by
	* If none of the values are valid, it will return the 1st element in the array
	* @param array $orders all valid order names
	* @return string order of recorset
	*/
	function get_value_order($orders = array(), $get_name = 'order') {
		if (empty($orders))		// Return null if the order array is empty
			return NULL;

		// Set default order value
		// If a value is specifed in GET, use that.  Else use the first element in the array
		$order = isset($_GET[$get_name]) ? $_GET[$get_name] : $orders[0];

		if (in_array($order, $orders))
			$order = $order;
		else
			$order = $orders[0];

		return $order;
	}


	/**
	* Opposite of php's nl2br function.
	* Subs in a newline for all brs
	* @param string $subject line to make subs on
	* @return reformatted line
	*/
	function br2nl($subject) {
		return str_replace('<br />', "\n", $subject);
	}

	/**
	* Writes a log string to the log file specified in config.php
	* @param string $string log entry to write to file
	* @param string $userid memeber id of user performing the action
	* @param string $ip ip address of user performing the action
	*/
	function write_log($string, $userid = NULL, $ip = NULL) {
		global $conf;
		$delim = "\t";
		$file = $conf['app']['logfile'];
		$values = '';

		if (!$conf['app']['use_log'])	// Return if we aren't going to log
			return;

		if (empty($ip))
			$ip = $_SERVER['REMOTE_ADDR'];

		clearstatcache();				// Clear cached results

		if (!is_dir(dirname($file)))
			mkdir(dirname($file), 0777);		// Create the directory

		if (!touch($file))
			return;					// Return if we cant touch the file

		if (!$fp = fopen($file, 'a'))
			return;					// Return if the fopen fails

		flock($fp, LOCK_EX);		// Lock file for writing
		if (!fwrite($fp, '[' . date('D, d M Y H:i:s') . ']' . $delim . $string . $delim . $userid . $delim . $ip . "\r\n"))	// Write log entry
        	return;					// Return if we cant write to the file
		flock($fp, LOCK_UN);		// Unlock file
		fclose($fp);
	}

	/**
	* Returns the day name
	* @param int $day_of_week day of the week
	* @param int $type how to return the day name (0 = full, 1 = one letter, 2 = two letter, 3 = three letter)
	*/
	function get_day_name($day_of_week, $type = 0) {
		global $days_full;
		global $days_abbr;
		global $days_letter;
		global $days_two;

		$names = array (
			$days_full, $days_letter, $days_two, $days_letter
			);

		return $names[$type][$day_of_week];
	}

	/**
	* Redirects a user to a new location
	* @param string $location new http location
	* @param int $time time in seconds to wait before redirect
	*/
	function redirect($location, $time = 0, $die = true) {
		header("Refresh: $time; URL=$location");
		if ($die) exit;
	}

	/**
	* Prints out the HTML to choose a language
	* @param none
	*/
	function print_language_pulldown() {
		global $conf;
		?>
		<select name="language" class="textbox" onchange="changeLanguage(this);">
		<?php
			$languages = get_language_list();
			foreach ($languages as $lang => $settings) {
				echo '<option value="' . $lang . '"'
					. ((determine_language() == $lang) ? ' selected="selected"' : '' )
					. '>' . $settings[3] . ($lang == $conf['app']['defaultLanguage'] ? ' ' . translate('(Default)') : '') . "</option>\n";
			}
		?>
		</select>
		<?php
	}

	/**
	* Searches the input string and creates links out of any properly formatted 'URL-like' text
	* Written by Fredrik Kristiansen (russlndr at online.no)
	* and Albrecht Guenther (ag at phprojekt.de).
	* @param string $str string to search for links to create
	* @return string with 'URL-like' text changed into clickable links
	*/
	function html_activate_links($str) {
		$str = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)', '<a href="\1" target="_blank">\1</a>', $str);
		$str = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)', '\1<a href="http://\2" target="_blank">\2</a>', $str);
		$str = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})','<a href="mailto:\1">\1</a>', $str);
		return $str;
	}

	/**
	 * Calculates the Difference between two timestamps
	 *
	 * @param integer $start_timestamp
	 * @param integer $end_timestamp
	 * @param integer $unit (default 0)
	 * @return string
	 * @access public
	 */
	function dateDifference($start_timestamp,$end_timestamp,$unit= 0){
	  $days_seconds_star= (23 * 56 * 60) + 4.091; // Star Day
	  $days_seconds_sun= 24 * 60 * 60; // Sun Day
	  $difference_seconds= $end_timestamp - $start_timestamp;
	  switch($unit){
	   case 3: // Days
		 $difference_days= round(($difference_seconds / $days_seconds_sun),2);
		 return 'approx. '.$difference_hours.' Days';
	   case 2: // Hours
		 $difference_hours= round(($difference_seconds / 3600),2);
		 return 'approx. '.$difference_hours.' Hours';
	   break;
	   case 1: // Minutes
		 $difference_minutes= round(($difference_seconds / 60),2);
		 return 'approx. '.$difference_minutes.' Minutes';
	   break;
	   default: // Seconds
		 if($difference_seconds > 1){
		   return $difference_seconds.' Seconds';
		 }
		 else{
		   return $difference_seconds.' Second';
		 }
	  }
	}

	/*	Author: Raju Mazumder
	 *	Email: rajuniit@gmail.com
	 *	Class: A simple class to export mysql query and whole html and php page to excel, doc, etc.
	 */
	function exportToExcel($data,$excel_file_name) {
    header("Pragma: no-cache");	//Prevent Caching
    header("Expires: 0");	//Expires and 0 mean that the browser will not cache the page on your hard drive
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
		header("Content-Disposition: attachment; filename=$excel_file_name");
		header("Content-Transfer-Encoding: binary");

		$body = "";
		foreach($data[0] as $fieldName=>$val) {
			//$body.="<th>".$fieldName."</th>";
			if (!empty($body)) {
				$body .= ",";
			}
			$body .= $fieldName;
		}
		$body .= "\r\n";
    foreach($data as $row) {
    	$newRow = true;
			foreach ($row as $field=>$value) {
				if (!$newRow) {
					$body .=",";
				} else {
					$newRow = false;
				}
				$body.=$value;
			}
			$body .= "\r\n";
    }
    echo $body;
	}
}


/**
 * A class for making time periods readable.
 *
 * This class allows for the conversion of an integer
 * number of seconds into a readable string.
 * For example, '121' into '2 minutes, 1 second'.
 *
 * If an array is passed to the class, the associative
 * keys are used for the names of the time segments.
 * For example, array('seconds' => 12, 'minutes' => 1)
 * into '1 minute, 12 seconds'.
 *
 * This class is plural aware. Time segments with values
 * other than 1 will have an 's' appended.
 * For example, '1 second' not '1 seconds'.
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.2.1
 * @link        http://aidanlister.com/repos/v/Duration.php
 */
class Duration
{
    /**
     * All in one method
     *
     * @param   int|array  $duration  Array of time segments or a number of seconds
     * @return  string
     */
    function toString ($duration, $periods = null)
    {
        if (!is_array($duration)) {
            $duration = Duration::int2array($duration, $periods);
        }

        return Duration::array2string($duration);
    }


    /**
     * Return an array of date segments.
     *
     * @param        int $seconds Number of seconds to be parsed
     * @return       mixed An array containing named segments
     */
    function int2array ($seconds, $periods = null)
    {
        // Define time periods
        if (!is_array($periods)) {
            $periods = array (
                    'years'     => 31556926,
                    'months'    => 2629743,
                    'weeks'     => 604800,
                    'days'      => 86400,
                    'hours'     => 3600,
                    'minutes'   => 60,
                    'seconds'   => 1
                    );
        }

        // Loop
        $seconds = (float) $seconds;
        foreach ($periods as $period => $value) {
            $count = floor($seconds / $value);

            if ($count == 0) {
                continue;
            }

            $values[$period] = $count;
            $seconds = $seconds % $value;
        }

        // Return
        if (empty($values)) {
            $values = null;
        }

        return $values;
    }


    /**
     * Return a string of time periods.
     *
     * @package      Duration
     * @param        mixed $duration An array of named segments
     * @return       string
     */
    function array2string ($duration)
    {
        if (!is_array($duration)) {
            return false;
        }

        foreach ($duration as $key => $value) {
            $segment_name = substr($key, 0, -1);
            $segment = $value . ' ' . $segment_name;

            // Plural
            if ($value != 1) {
                $segment .= 's';
            }

            $array[] = $segment;
        }

        $str = implode(', ', $array);
        return $str;
    }
}
?>
